<template>
  <div>
    <el-form label-width="80px">
      <el-form-item label="头像">
        <img :src="loginInfo.avatar" style="float:left;width: 145px;height: 145px;border-radius: 10px">
        <!--上传组件开始-->
        <el-upload
            style="float: left;margin-left: 10px"
            action="http://localhost:29112/upload/image/user-avatar"
            name="file"
            :limit=1
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="imageUrl" alt="">
        </el-dialog>
        <!--上传组件结束-->
      </el-form-item>

      <el-form-item label="昵称">
        <el-input style="width: 200px;float: left" v-model="loginInfo.nickname" placeholder="请输入昵称" :maxlength="12"></el-input>
      </el-form-item>
      <el-form-item label="用户名" :error="hasShowErrorUsername">
        <el-input style="width: 200px;float:left;" v-model="loginInfo.username" disabled :maxlength="8" @input="validateInputUser"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" :error="hasShowErrorEmail">
        <el-input style="width: 200px;float:left;" v-model="loginInfo.email" placeholder="请输入邮箱" @input="validateEmail"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save()" style="margin-left: -450px">保存信息</el-button>
      </el-form-item>
    </el-form>
<!--    不用footer 可直接在HomeView中修改引用-->
  </div>
</template>

<script>
// import BaseUrl from "@/http/BaseUrl";
import TokenAxios from "@/http/TokenAxios";
import BaseUrl from "@/http/BaseUrl";

export default {
  data(){
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      loginInfo: localStorage.loginInfo ? JSON.parse(localStorage.loginInfo) : null,
      // loginInfo:JSON.parse(localStorage.getItem('loginInfo')) || {},
      imageUrl: '',
      param: {imgeUrl:''},
      hasShowErrorEmail:false,
      hasShowErrorUsername:false,
    }
  },
  methods: {
    handleRemove(file, fileList) {
      this.imageUrl="";
      console.log(file,fileList);
      let url='http://localhost:19780/upload/image/user-avatar/remove?url='+file.response.data;
      TokenAxios.post(url).then((response)=>{
        if (response.data.state===20000){
          this.$message.success("服务器文件删除成功");
        }
      })
    },
    handlePictureCardPreview(file) {
      debugger
      this.imageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(response,file,fileList){
      debugger
      console.log(response.data)
      this.imageUrl=response.data.url;
      console.log(this.imageUrl)
    },
    save() {
     if (this.hasShowErrorUsername!=false||this.hasShowErrorEmail!=false){
       this.$message({
         message: '请检查数据格式！',
         type: 'error'
       })
     }else {
       //判断是否选择了新的头像
       if (this.imageUrl!=""){
         this.loginInfo.avatar = this.imageUrl;
       }
       //发出修改请求
       debugger
       console.log(this.imageUrl)
       this.param.imgeUrl=this.loginInfo.avatar;

       debugger
       TokenAxios.post(BaseUrl.INFORMATIOON+'/users/avatar',this.param).then((response)=>{
         debugger
         if (response.data.state===20000){
           debugger
           response.data.imgeUrl=this.imageUrl;
           this.$message.success("修改成功!");
           //把修改完用户信息的数据 重新赋值给localStorage.user
           localStorage.loginInfo = JSON.stringify(this.loginInfo);
           debugger
         }
       })

       TokenAxios.post(BaseUrl.INFORMATIOON+'/users/update',this.loginInfo).then((response) => {
         debugger
         let json=response.data
         if(json.state==20000){
           this.$message.success("修改成功!");
           //把修改完用户信息的数据 重新赋值给localStorage.user
           localStorage.loginInfo = JSON.stringify(this.loginInfo);
           /* localStorage.setItem('loginInfo',JSON.stringify(this.loginInfo))*/
         }
       })
     }
    },
    validateEmail() {
      const regex = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
      if (!regex.test(this.loginInfo.email)) {
        this.hasShowErrorEmail = "邮箱格式有误!"
      }else {
        this.hasShowErrorEmail = false;
      }
    },
    validateInputUser() {
      const regex = /^[^\s!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]+$/;
      if (!regex.test(this.loginInfo.username)) {
        // this.loginInfo.username = this.loginInfo.username.replace(/[^\w\s]/gi, '');
        this.hasShowErrorUsername = "用户名格式错误！"
      }else {
        this.hasShowErrorUsername =false;
      }
    },
    },
  created() {

  },
  mounted() {

  }
}
</script>
<style>
body{
  background-color: rgb(241,242,243);
}
</style>
